//
//  ReleaseManageViewController.m
//  Logistics
//
//  Created by hy002 on 2018/1/19.
//  Copyright © 2018年 HUYU. All rights reserved.
//

#import "ReleaseManageViewController.h"
#import "HallModel.h"

static NSString *cellPath = @"ReleaseManageCell";
@interface ReleaseManageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)NSArray *allHallAry;

@end

@implementation ReleaseManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
    [self requestAllHallList];
}

- (void)creatUI
{
    self.navigationItem.title = @"发布管理";
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = BacksColor;
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT-SCREER_HeiX) style:UITableViewStylePlain];
    _tableview.backgroundColor = BacksColor;
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableview];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _allHallAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = BacksColor;
    if (_allHallAry.count > 0)
    {
        HallModel *haModel = _allHallAry[indexPath.row];
        CGFloat W = SCREEN_WIDTH-20*displaywidth;
        UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(10*displaywidth, 10*displaywidth, W, 120*displaywidth)];
        whiteView.backgroundColor = [UIColor whiteColor];
        whiteView.layer.cornerRadius = 5*displaywidth;
        whiteView.layer.masksToBounds = YES;
        [cell.contentView addSubview:whiteView];
        CGFloat Width = 0;
        UILabel *startLab = [ZCControl createLabelWithFrame:CGRectMake(10*displaywidth, 10*displaywidth, 160*displaywidth, 25*displaywidth) Font:15.0 Text:[NSString stringWithFormat:@"%@%@%@",haModel.shipmentcity,haModel.shipmentdistrict,haModel.shipmentname] Color:BlaTitColor];
        CGRect bound1 = [startLab.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, 25*displaywidth) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:NormalFontWithSize(15.0+fontScale) forKey:NSFontAttributeName] context:nil];
        startLab.frame = CGRectMake(10*displaywidth, 10*displaywidth, bound1.size.width+5*displaywidth, 25*displaywidth);
        Width += (bound1.size.width+15*displaywidth);
        [whiteView addSubview:startLab];
        UIImageView *imgBView = [ZCControl createImageViewWithFrame:CGRectMake(Width, 15*displaywidth, 30*displaywidth, 15*displaywidth) ImageName:@"zxli_icon01"];
        imgBView.contentMode = UIViewContentModeScaleAspectFit;
        [whiteView addSubview:imgBView];
        Width+=30*displaywidth;
        UILabel *stopLab = [ZCControl createLabelWithFrame:CGRectMake(Width, 10*displaywidth, W-Width-10*displaywidth, 25*displaywidth) Font:14.0 Text:[NSString stringWithFormat:@"%@%@%@",haModel.destinationCity,haModel.destinationDistrict,haModel.destinationname] Color:BlaTitColor];
        [whiteView addSubview:stopLab];
        NSString *volStr = haModel.cargoType;
        if (![haModel.weight isEqualToString:@""])
        {
            volStr = [volStr stringByAppendingString:[NSString stringWithFormat:@"/%@吨",haModel.weight]];
        }
        if (![haModel.volume isEqualToString:@""])
        {
            volStr = [volStr stringByAppendingString:[NSString stringWithFormat:@"/%@立方",haModel.volume]];
        }
        UILabel *volumeLab = [ZCControl createLabelWithFrame:CGRectMake(10*displaywidth, 35*displaywidth, W/2, 20*displaywidth) Font:13.0 Text:volStr Color:YellowColor];
        [whiteView addSubview:volumeLab];
        
        NSString *lenStr = @"不限长度";
        if (![haModel.length isEqualToString:@""])
        {
            lenStr = string(haModel.length, @"米");
            
        }
        if (![haModel.truckType isEqualToString:@""])
        {
            lenStr = [lenStr stringByAppendingString:[NSString stringWithFormat:@"/%@",haModel.truckType]];
        }
        if ([haModel.baffle integerValue] == 1)
        {
            lenStr = [lenStr stringByAppendingString:@"/需尾板"];
        }
        if ([haModel.baffle integerValue] == 0)
        {
            lenStr = [lenStr stringByAppendingString:@"/无需尾板"];
        }
        UILabel *lentnLab = [ZCControl createLabelWithFrame:CGRectMake(W/2+10*displaywidth, 35*displaywidth, W/2-20*displaywidth, 25*displaywidth) Font:13.0 Text:lenStr Color:RGB(187, 187, 187)];
        lentnLab.textAlignment = NSTextAlignmentRight;
        [whiteView addSubview:lentnLab];
        
        NSString *timeStr = haModel.createDtm;
        if (timeStr.length > 10)
        {
            timeStr = [timeStr substringToIndex:10];
        }
        UILabel *timeLab = [ZCControl createLabelWithFrame:CGRectMake(10*displaywidth, 55*displaywidth, W/2, 20*displaywidth) Font:13.0 Text:timeStr Color:RGB(141, 147, 154)];
        [whiteView addSubview:timeLab];
        UILabel *priceLab = [ZCControl createLabelWithFrame:CGRectMake(W-160*displaywidth, 55*displaywidth, 150*displaywidth, 20*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@"￥%@元左右",haModel.price] Color:RGB(189, 189, 189)];
        priceLab.textAlignment = NSTextAlignmentRight;
        NSMutableAttributedString *title2 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%@元左右",haModel.price]];
        NSRange titleRange2 = [[title2 string]rangeOfString:[NSString stringWithFormat:@"￥%@",haModel.price]];
        [title2 addAttribute:NSForegroundColorAttributeName value:YellowColor range:titleRange2];
        priceLab.attributedText = title2;
        [whiteView addSubview:priceLab];
        
        UIView *lineAview = [[UIView alloc] initWithFrame:CGRectMake(10*displaywidth, 80*displaywidth, W-20*displaywidth, 1*displaywidth)];
        lineAview.backgroundColor = BacksColor;
        [whiteView addSubview:lineAview];
        
        UILabel *postLab = [ZCControl createLabelWithFrame:CGRectMake(10*displaywidth, 85*displaywidth, W-110*displaywidth, 30*displaywidth) Font:13.0 Text:haModel.cargoTagname Color:BlaTitColor];
        [whiteView addSubview:postLab];
        NSString *titString = @"删除";
        UIButton *btn = [ZCControl createButtonWithFrame:CGRectMake(W-100*displaywidth, 85*displaywidth, 90*displaywidth, 30*displaywidth) ImageName:nil Target:self Action:@selector(delecteAction:) Title:titString Font:13.0];
        btn.backgroundColor = [UIColor clearColor];
        [btn setTitleColor:ThemeColor forState:UIControlStateNormal];
        btn.contentVerticalAlignment = 0;
        btn.contentHorizontalAlignment = 2;
        btn.tag = 1000+indexPath.row;
        [whiteView addSubview:btn];
        
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130*displaywidth;
}

- (void)delecteAction:(UIButton *)btn
{
    NSInteger index = btn.tag-1000;
    HallModel *model = _allHallAry[index];
    WEAKSELF;
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定删除该条货源信息" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf requestDeleteCargo:s_str(model.ID)];
    }];
    [alertVC addAction:cancelAction];
    [alertVC addAction:okAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}

- (void)requestDeleteCargo:(NSString *)idstr
{
    WEAKSELF;
    NSDictionary *dic = @{@"id":idstr};
    [HYBaseHandler requestWithTheFormUrl:SERVER_cargoDelete Parmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            [LTools showMBProgressWithText:@"删除成功" addToView:weakSelf.view];
            [weakSelf requestAllHallList];
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}


//货源信息
- (void)requestAllHallList
{
    WEAKSELF;
    NSDictionary *dic = @{@"size":@"9999"};
    [HYBaseHandler requestWithTheFormUrl:SERVER_cargoMine Parmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            weakSelf.allHallAry = [HallModel mj_objectArrayWithKeyValuesArray:obj[@"data"][@"content"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableview reloadData];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
